package com.water.wfo.component.junit;

import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * packageName com.water.wfo.component.junit
 *
 * @author junhuili
 * @version JDK 8
 * @date 2024/5/27
 * @description TODO
 */
@EnableConfigurationProperties
@ComponentScan(basePackages = {"com.water"})
@PropertySource("application.properties")
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseTest {
    public static final Properties JUNITPROPERTIES = new Properties();

    public BaseTest(){}
    public Object fillField(Class<?> clazz){
        try {
            Object object = clazz.newInstance();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method m : methods){
                try {
                    Class<?>[] types = m.getParameterTypes();
                    if(types.length >0){
                        for (Class<?> t : types){
                            if(Byte.class.getName().equalsIgnoreCase(t.getName())
                                    || Integer.class.getName().equalsIgnoreCase(t.getName())
                                    || Float.class.getName().equalsIgnoreCase(t.getName())
                                    || Double.class.getName().equalsIgnoreCase(t.getName())
                                    || "int".equalsIgnoreCase(t.getName())){
                                m.invoke(object,0);
                            }else if(Short.class.getName().equalsIgnoreCase(t.getName())
                                    || "short".equalsIgnoreCase(t.getName())){
                                m.invoke(object,(short)0);
                            }else if(Long.class.getName().equalsIgnoreCase(t.getName())
                                    || "long".equalsIgnoreCase(t.getName())){
                                m.invoke(object,0L);
                            }else if(Boolean.class.getName().equalsIgnoreCase(t.getName())
                                    || "boolean".equalsIgnoreCase(t.getName())){
                                m.invoke(object,false);
                            }else if(Character.class.getName().equalsIgnoreCase(t.getName())
                                    || "char".equalsIgnoreCase(t.getName())){
                                m.invoke(object,'\n');
                            }else if(String.class.getName().equalsIgnoreCase(t.getName())){
                                m.invoke(object,"A");
                            }else if(Date.class.getName().equalsIgnoreCase(t.getName())){
                                m.invoke(object,new Date());
                            }else if(BigDecimal.class.getName().equalsIgnoreCase(t.getName())){
                                m.invoke(object,new BigDecimal(0));
                            }else if(List.class.getName().equalsIgnoreCase(t.getName())){
                                m.invoke(object,new ArrayList());
                            }else {
                                m.invoke(clazz.newInstance(),null);
                            }
                        }
                    }else {
                        m.invoke(clazz.newInstance(),null);
                    }
                }catch (IllegalAccessException e2){

                }catch (IllegalArgumentException e1){

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            return object;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    static {
        try {
            InputStream resource = BaseTest.class.getResourceAsStream("/application.properties");
            JUNITPROPERTIES.load(resource);

           /* InputStream resourceExtra = BaseTest.class.getResourceAsStream("/system-config-extra.properties");
            if(null != resourceExtra) {
                JUNITPROPERTIES.load(resourceExtra);
            }*/

        } catch (Exception e) {
            Assert.fail("单元测试配置system-config.properties加载失败");
        }
    }
}
